//
//  UserEditeImageView.m
//  WULIHH
//
//  Created by tang on 16/5/19.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "UserEditeImageView.h"
#import "Header.h"

@interface UserEditeImageView ()
{
    float viewWith;
    float viewHeight;
    
    UIImageView *_backImageView;
    
}

@end



@implementation UserEditeImageView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        viewWith = self.bounds.size.width;
        viewHeight = self.bounds.size.height;
        
        _backImageView = [[UIImageView alloc] init];
        [self addSubview:_backImageView];
        
    }
    return self;
}




- (void)initUI
{
    
}

//展示图片
- (void)setImage:(UIImage *)image
{
    [_backImageView setImage:image];
    
    
//判断图片大小
    float scale = 0.0;
    
    float imageWith = image.size.width/2.0;
    float imageHeight = image.size.height/2.0;
    
    if (imageWith > viewWith && imageHeight > viewHeight)//长宽都是大图
    {
        if (imageHeight > imageWith)
        {
            scale = viewWith/imageHeight;
            [_backImageView setFrame:CGRectMake(0, 0, viewWith*scale, viewHeight)];
            
        }
        else
        {
            scale = viewWith/imageWith;
            [_backImageView setFrame:CGRectMake(0, 0, viewWith, viewHeight*scale)];
        }
    }
    else if (imageHeight >= viewHeight && imageWith <= viewWith)    //长图
    {
        scale = viewHeight/imageHeight;
        [_backImageView setFrame:CGRectMake(0, 0, viewWith*scale, viewHeight)];
    }
    else if (imageWith >= viewWith && imageHeight < viewHeight)
    {
        scale = viewWith/imageWith;
        [_backImageView setFrame:CGRectMake(0, 0, viewWith, viewHeight*scale)];
    }
    else    //小图
    {
        [_backImageView setFrame:CGRectMake(0, 0, imageWith, imageHeight)];
    }
    
    [_backImageView setCenter:CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0)];
}


#pragma mark---保存图片


@end
